package com.lzw.mall.order.controller;


import com.lzw.mall.common.entity.Product;
import com.lzw.mall.common.service.IAttachmentService;
import com.lzw.mall.common.service.IProductService;
import com.lzw.mall.common.vo.IndexProductVO;
import com.lzw.mall.common.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService productService;

    @Autowired
    private IAttachmentService attachmentService;

    @Autowired
    private ICartService cartService;

    @GetMapping("/dianxin")
    @ResponseBody
    public List<IndexProductVO> dianxin(){
        return productService.dianxins();
    }

    @GetMapping("/jianguo")
    @ResponseBody
    public List<IndexProductVO> jianguo() {
        return productService.jianguos();
    }

    @GetMapping("detail")
    public String detail(Integer id, Model model){
        Product p=productService.getById(id);
        model.addAttribute("product",p);
        model.addAttribute("imgs",attachmentService.selectList(id,"product"));
        return "detail";
    }

    @ResponseBody
    @PostMapping("/cart")
    public void addToCart(Integer productId,Integer amount){
        cartService.addToCart(productId,amount);
    }
}
